package com.cazor.dg.common;

import cn.hutool.core.collection.CollectionUtil;
import com.cazor.dg.domain.DgConfigContentType;
import com.cazor.dg.domain.DgDeviceSensor;
import com.cazor.dg.domain.DgMonitorStation;
import com.cazor.dg.service.IDgConfigContentTypeService;
import com.cazor.dg.service.IDgDeviceSensorService;
import com.cazor.dg.service.IDgMonitorStationService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DgSysCacheService {

    @Resource
    private IDgConfigContentTypeService configContentTypeService;
    @Resource
    private IDgMonitorStationService monitorStationService;
    @Resource
    private IDgDeviceSensorService deviceSensorService;


    @PostConstruct
    public void init(){
        this.initDbConfigContentType();
        this.initDgMonitorStation();
        this.initDgDeviceSensor();
    }

    private final List<DgConfigContentType> configContentTypeList = new ArrayList<>();
    private final Map<String, DgConfigContentType>  configContentTypeMap = new HashMap<>();
    /**
     * 初始化 监测内容类型数据读取配置对象
     */
    private void initDbConfigContentType() {
        this.configContentTypeList.clear();
        this.configContentTypeMap.clear();
        this.configContentTypeList.addAll(
                configContentTypeService.lambdaQuery()
                .eq(DgConfigContentType::getValid, "1")
                .list());
        if (CollectionUtil.isNotEmpty(this.configContentTypeList)) {
            this.configContentTypeMap.putAll(
                this.configContentTypeList.stream().collect(Collectors.toMap(DgConfigContentType::getContentType, item->item))
            );
        }
    }


    /**
     * 根据 监测内容类型的名称 获取 配置对象
     * @param contentType 监测内容类型 名称
     * @return 配置对象
     */
    public DgConfigContentType getConfigContentType(String contentType) {
        return this.configContentTypeMap.get(contentType);
    }

    public String getContentTypeValue(String contentType) {
        DgConfigContentType obj = this.getConfigContentType(contentType);
        return obj!=null ? obj.getContentTypeValue() : contentType;
    }

    // ------------------------------------------------------------------------------------------------

    private final List<DgMonitorStation> stationList = new ArrayList<>();
    private final Map<String, DgMonitorStation> stationMap = new HashMap<>();

    /**
     * 初始化 监测站 数据
     */
    private void initDgMonitorStation() {
        this.stationList.clear();
        this.stationMap.clear();
        this.stationList.addAll(
                monitorStationService.lambdaQuery().eq(DgMonitorStation::getValid, "1")
                        .eq(DgMonitorStation::getStatus, 1)
                        .list()
        );
        if (CollectionUtil.isNotEmpty(stationList)) {
            this.stationMap.putAll(
                    this.stationList.stream().collect(Collectors.toMap(DgMonitorStation::getStationId, item->item))
            );
        }
    }

    /**
     * 根据监测站id 获取监测站 数据
     * @param stationId 监测站id
     * @return 监测站数据
     */
    public DgMonitorStation getMonitorStation(String stationId) {
        return this.stationMap.get(stationId);
    }


    private final List<DgDeviceSensor> sensorList = new ArrayList<>();
    private final Map<String, DgDeviceSensor> sensorMap = new HashMap<>();

    private void initDgDeviceSensor() {
        this.sensorList.clear();
        this.sensorMap.clear();
        this.sensorList.addAll(
                deviceSensorService.lambdaQuery().eq(DgDeviceSensor::getValid, "1")
                        .eq(DgDeviceSensor::getStatus, 1)
                        .list()
        );
        if (CollectionUtil.isNotEmpty(sensorList)) {
            this.sensorMap.putAll(
                    this.sensorList.stream().collect(Collectors.toMap(DgDeviceSensor::getSensorId, item->item))
            );
        }
    }

    /**
     * 根据 传感器id 获取传感器数据
     * @param sensorId 传感器id
     * @return 传感器数据
     */
    public DgDeviceSensor getDeviceSensor(String sensorId) {
        return this.sensorMap.get(sensorId);
    }

}
